home *** CD-ROM | disk | FTP | other *** search
- unit LottryU2;
-
- interface
-
- uses
- WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, ExtCtrls;
-
- type
- TfrmLottery = class(TForm)
- lblNo1: TLabel;
- lblNo2: TLabel;
- lblNo3: TLabel;
- lblNo4: TLabel;
- lblNo5: TLabel;
- lblNo6: TLabel;
- pnlDisplay: TPanel;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure GenericClick(Sender: TObject);
- private
- List: TList;
- end;
-
- var
- frmLottery: TfrmLottery;
-
- implementation
-
- {$R *.DFM}
-
- {$ifndef Win32}
- procedure Sleep(MSec: Integer);
- var
- OldTime: TDateTime;
- begin
- OldTime := Now;
- repeat until Now >= OldTime + MSec / MSecsPerDay
- end;
- {$endif}
-
- procedure AnimateLabel(Lbl: TLabel; Loops, Delay: Integer);
- var
- Loop: Integer;
- const
- Chars: String = '/-\I';
- begin
- for Loop := 1 to Loops do
- begin
- Lbl.Caption := Chars[Loop mod Length(Chars) + 1];
- { Post label's parent a repaint message }
- Lbl.Parent.Invalidate;
- { Allow all posted messages to be processed }
- Application.ProcessMessages;
- { Let user bail out if bored }
- if Application.Terminated then Break;
- Sleep(Delay)
- end
- end;
-
- procedure SortNumbers(Lbls: array of TLabel);
- var
- Loop, Loop2: Integer;
- Tmp: String;
- begin
- for Loop := Low(Lbls) to High(Lbls) do
- for Loop2 := Loop + 1 to High(Lbls) do
- if StrToInt(Lbls[Loop].Caption) > StrToInt(Lbls[Loop2].Caption) then
- begin
- Tmp := Lbls[Loop].Caption;
- Lbls[Loop].Caption := Lbls[Loop2].Caption;
- Lbls[Loop2].Caption := Tmp
- end
- end;
-
- procedure TfrmLottery.FormCreate(Sender: TObject);
- begin
- Randomize;
- List := TList.Create
- end;
-
- procedure TfrmLottery.FormDestroy(Sender: TObject);
- begin
- List.Free;
- List := nil
- end;
-
- procedure TfrmLottery.GenericClick(Sender: TObject);
- var
- Loop, CharLoop, Index: Integer;
- const
- MaxNum = 49;
- { Re-entry protection flag }
- InHandler: Boolean = False;
- begin
- if not InHandler then
- begin
- InHandler := True;
- { Empty the list }
- List.Clear;
- { Fill the list with 49 numbers }
- for Loop := 1 to MaxNum do
- List.Add(Pointer(Loop));
- { Loop for each number sought }
- for Loop := 1 to 6 do
- begin
- { Choose one of the remaining numbers in the list }
- Index := Random(List.Count);
- { Do "pretty" animation }
- AnimateLabel(FindComponent('lblNo' + IntToStr(Loop)) as TLabel, 15, 50);
- { Write it in the appropriate label }
- TLabel((FindComponent('lblNo' + IntToStr(Loop)))).Caption :=
- IntToStr(Longint(List[Index]));
- { Remove the number from the list, so it won't be picked again }
- List.Delete(Index);
- end;
- ShowMessage('Press OK to sort the numbers into numerical order');
- SortNumbers([lblNo1, lblNo2, lblNo3, lblNo4, lblNo5, lblNo6]);
- InHandler := False
- end;
- end;
-
- end.
-